<?php

namespace library\Session;

/**
 * Klasa fabryczna tworząca sterownik do obsługi sesji
 *
 * @author Damian Jóźwiak
 */
class Session extends \library\Base{
    /**
     * @readwrite
     */
    protected $type;
    
    /**
     * @readwrite;
     */
    protected $options;
    
    public function initialize(){
        $type = $this->getType();
        if (empty($type)){
          
            $configuration = \library\Registry::get("configuration");
            $host ="default";
            switch ($_SERVER['HTTP_HOST']){
                case "localhost:8080": {$host ="default"; break;}
                case "alequizdev.pl": {$host="quiz";break;}
            }
            /* @var $configuration \library\configuration\Ini */
           if($configuration){
               $configuration=$configuration->initialize();
               $parsed = $configuration->parse(APP_PATH."/application/configuration/conf");
               if(!empty($parsed->session->$host) && !empty($parsed->session->$host->type)){
                   $type = $parsed->session->$host->type;
                   unset($parsed->session->$host->type);
                   $this->__construct(array(
                       "type"=>$type,
                       "options" =>(array)$parsed->session->$host
                   ));
               }
            }      
       }
        if (!$this->type){
            throw new \model\exceptions\ExceptionWrongDatabaseDriver();
        }
        switch ($this->type){
            case "server":{
                return new Server($this->options);
            }
            default :{
                throw new \library\exceptions\ExceptionWrongDriver();
            }
        }
    }
}
